<?php

/* * *********************************************************** */
/* * ********************** Classe bannerPrincipalDao ********************* */
/* * ************ Desenvolvido por Antônio Ezequiel ************** */
/* * *********************************************************** */

class BannerPrincipalDao extends BannerPrincipal {

    public function adicionarBanner() {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query("insert into tbl_banner_principal (bnp_titulo, bnp_link, bnp_imagem)
                                values ('$this->bnp_titulo','$this->bnp_link','$this->bnp_imagem')");

        if ($queri) {
            $id = parent::get_insert_id();
            parent::desconectarBD();
            return $id;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function listarBannersPaginado($inicio, $porPagina) {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query("select bnp_titulo as titulo, bnp_link as link, bnp_codigo as codigo, bnp_imagem as imagem 
                                from tbl_banner_principal order by bnp_codigo desc limit $inicio, $porPagina");

        if ($queri) {
            $dados = parent::fetch_rows($queri);
            parent::desconectarBD();
            return $dados;
        } else {
            parent::desconectarBD();
            return false;
        }
    }
    
    public function listarBannersInicio() {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query("select bnp_titulo as titulo, bnp_link as link, bnp_codigo as codigo, bnp_imagem as imagem 
                                from tbl_banner_principal order by bnp_codigo desc limit 0, 3");

        if ($queri) {
            $dados = parent::fetch_rows($queri);
            parent::desconectarBD();
            return $dados;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function totalPaginas() {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query("select count(bnp_codigo) as quantidade from tbl_banner_principal");

        if ($queri) {
            $dados = $queri->fetch_assoc();
            parent::desconectarBD();
            return $dados;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function listarBannersPaginadoAjax($ini, $codigo, $inicio, $porPagina) {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query(sprintf("select bnp_titulo as titulo, bnp_link as link, bnp_codigo as codigo
                                        from tbl_banner_principal where %s %s %s LIMIT $inicio, $porPagina", $ini, $codigo, $this->bnp_titulo));

        if ($queri) {
            $dados = parent::fetch_rows($queri);
            parent::desconectarBD();
            return $dados;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function totalPaginasAjax($ini, $codigo) {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query(sprintf("select count(*) as totRegistros from tbl_banner_principal
                                        where %s %s %s", $ini, $codigo, $this->bnp_titulo));

        if ($queri) {
            $dados = $queri->fetch_assoc();
            parent::desconectarBD();
            return $dados;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function deletarBanner($codigo) {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query("delete from tbl_banner_principal where bnp_codigo = '$codigo'");

        if ($queri) {
            parent::desconectarBD();
            return true;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function listarBanner($codigo) {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query("select bnp_codigo as codigo, bnp_titulo as titulo, bnp_link as link, bnp_imagem as imagem from tbl_banner_principal
                                where bnp_codigo ='$codigo'");

        if ($queri) {
            $dados = $queri->fetch_assoc();
            parent::desconectarBD();
            return $dados;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function atualizarBanner($codigo) {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query("update tbl_banner_principal set bnp_titulo = '$this->bnp_titulo', bnp_imagem = '$this->bnp_imagem',
                                bnp_link = '$this->bnp_link' where bnp_codigo = '$codigo'");

        if ($queri) {
            parent::desconectarBD();
            return true;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

}

?>
